package com.ruoyi.task;

import cn.hutool.http.HttpUtil;
import com.ruoyi.web.core.config.WeChatTemplateMsg;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Component
@Configuration
@EnableScheduling
@Slf4j
public class WhatSendMesTask {
    public static final String APP_ID = "wxe4eb4267e98b952a";

    public static final String APP_SECRET = "9d7dbf01572eef6a82f60a078c7cd69e";

    public static final String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APP_ID +"&secret=" + APP_SECRET;

    //@Scheduled(cron = "0 0 0 */1 * ?") //正式使用时每天凌晨1点执行一次： 0 0 0 */1 * ?
    public void sendMessage(){
        String res = HttpUtil.get(requestUrl);
        JSONObject jsonObject = JSONObject.fromObject(res);
        String accessToken = jsonObject.getString("access_token");
        RestTemplate restTemplate = new RestTemplate();
        String requestUrl =  "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+ accessToken;
        ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, null, String.class);
        log.info("结果是: {}",response.getBody());
        com.alibaba.fastjson.JSONObject result = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
        com.alibaba.fastjson.JSONArray openIdJsonArray = result.getJSONObject("data").getJSONArray("openid");
        Iterator iterator = openIdJsonArray.iterator();
        if (iterator.hasNext()){
            log.debug("用户openid："+iterator.next());
            // 模板参数
            Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>();
            // openId代表一个唯一微信用户，即微信消息的接收人
            String openId = iterator.toString();
            // 微信的基础accessToken
            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
            //换为自己的模板id；以及对应模板里面的消息体
            // 公众号的模板id(也有相应的接口可以查询到)
            String templateId = "yBVd_hZR1q5-hko3eP9BsuFfpYnFxxxxxxxxxxxxxx";
            //sendMag.put("city", new WeChatTemplateMsg("广州"));


            //拼接base参数
            Map<String, Object> sendBody = new HashMap<>();
            sendBody.put("touser", openId);               // openId
            sendBody.put("url", "https://www.baidu.com");  //跳转网页url
            sendBody.put("data", sendMag);                   // 模板参数
            sendBody.put("template_id", templateId);      // 模板Id
            ResponseEntity<String> responseMes = restTemplate.postForEntity(url, sendBody, String.class);
            log.info("结果是: {}",response.getBody());
            com.alibaba.fastjson.JSONObject jsonObjectMes = com.alibaba.fastjson.JSONObject.parseObject(responseMes.getBody());
            String messageCode = jsonObjectMes.getString("errcode");
            String msgId = jsonObjectMes.getString("msgid");
            System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
        }

    }


}
